#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :hippopx_20240524.py
# @Time      :2024/5/24 
# @Author    :CL
# @email     :1037654919@qq.com
import os
import time


import requests
from bs4 import BeautifulSoup
import json
from utils import proxies, mongo_manager
images_keywords  =  mongo_manager("images_keywords", db='car_images')
hippopx_images = mongo_manager("hippopx_images", db="car_images")
def get_kw_image(kw='cat', pageNo=1):
    headers = {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "priority": "u=0, i",
        "referer": "https://www.hippopx.com/zh",
        "sec-ch-ua": "\"Chromium\";v=\"124\", \"Google Chrome\";v=\"124\", \"Not-A.Brand\";v=\"99\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Linux\"",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "same-origin",
        "sec-fetch-user": "?1",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
    }
    cookies = {
        "_ga": "GA1.2.199250898.1709189945",
        "_ga_DXD0KCJXGN": "GS1.2.1710747771.17.0.1710747771.0.0.0",
        "__gads": "ID=2212df6124fd1027:T=1709189947:RT=1716541608:S=ALNI_MYZlWbgDuwJhPap7JFLVvAl_nAVKQ",
        "__gpi": "UID=00000dc4e3f7df47:T=1709189947:RT=1716541608:S=ALNI_Mb1evSZwCU8sO2DGcLMTWq_gLI7sA",
        "__eoi": "ID=8496e1cf3c136ff1:T=1709189947:RT=1716541608:S=AA-Afjasc5apup3uysis9GE0wret",
        "FCNEC": "%5B%5B%22AKsRol-3O8YmMTr42ZkPVNAbh9Gkc_D2WU0mwc354KRhDpmKeJ7871M5n0rOXzui8OiOPKACyGclP1ltLThiwgqYt4AyqrjeUPAOeLXSFNp4uUbRgh2iuxeenNrVRBTM3KebTVWrjAJQNR05AFCOL58scSlsAR4hJQ%3D%3D%22%5D%2Cnull%2C%5B%5B2%2C%22%5Bnull%2C%5Bnull%2C1%2C%5B1709881927%2C531495000%5D%5D%5D%22%5D%5D%5D"
    }
    url = "https://www.hippopx.com/zh/search"
    params = {
        "q": kw,
        "page": pageNo
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)
    print(response.url,response)
    requests.session().close()
    if response.text:
        return response.text


def down_image(file, url):  # 下载图片方法
    print("开始下载：", url)
    for i in range(5):
        try:
            response = requests.get(url, proxies=proxies, timeout=10)
            with open(file, 'wb') as fd:
                fd.write(response.content)
            requests.session().close()
            return 1
        except Exception as e:
            time.sleep(5)
    print("下载失败了", url)
    if os.path.exists(file):
        os.remove(file)
    return 0


def get_pictrue(keyword):
    path = f'/media/chenglei3/77D014CED257D1411/images/hippopx/{keyword}/'
    os.makedirs(path, exist_ok=True)
    page = 1
    while page <= 100:
        response = get_kw_image(keyword, page)
        soups = BeautifulSoup(response, 'lxml')
        datas = soups.find_all('figure')
        print('len(datas):',page, len(datas))
        page += 1
        for data in datas:
            id = data.find('a')['href'].split('-')[-1]  # 获取id 也就是名字
            if hippopx_images.exist({"id": id}):  # 判断是否已经下载 #'stutus': {"$exists": True}
                continue
            title = data.find('img')['title']
            src = data.find('img')['src']
            img_houzhui = src.split('.')[-1]
            result = down_image(file=f'{path}{id}.{img_houzhui}', url=src)
            if result == 0:  # 下载失败，跳出
                continue

            json_data = {"_id": id,  "image_url": src,
                         "label": title, "desc": ''}
            json_data['stutus'] = 'success'
            hippopx_images.updateOne({'_id': json_data['_id']}, json_data)
        if len(datas) < 30:
            break

keywords=[]

people_keywords = [
    "车内中国婴儿", "中国婴儿宝宝照",
    "车内中国儿童男", "中国儿童男",
    "车内中国儿童女", "中国儿童女",
    "车内中国成年人男", "中国成年人男",
    "车内中国成年人女", "中国成年人女",
    "车内中国老年人男", "中国老年人男人像",
    "车内中国老年人女", "中国老年人女人像"
]
if __name__ == '__main__':
    print()
    for keyword in people_keywords:
        get_pictrue(keyword)

    # print(get_kw_image())
